package com.wyz.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyz.common.dto.OrderDTO;
import com.wyz.common.po.OrderPO;
import com.wyz.common.po.UserPO;
import com.wyz.common.vo.OrderAvailableVO;
import com.wyz.common.vo.OrderDetailVO;
import com.wyz.common.vo.OrderPublishedVO;
import com.wyz.common.vo.OrderPickedVO;
import com.wyz.web.mapper.OrderRepository;
import com.wyz.web.mapper.UserRepository;

import com.wyz.web.service.OrderService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private OrderRepository orderRepository;

    private static ObjectMapper mapper = new ObjectMapper();

    @Override
    public List<OrderAvailableVO> getAllUnpickOrders() {
        List<OrderPO> orderPOs = orderRepository.selectList(new QueryWrapper<OrderPO>().lambda().eq(OrderPO::getStatus,"未接单"));
        List<OrderAvailableVO> commonOrderVOs = new ArrayList<>();
        for (OrderPO orderPO : orderPOs) {
            OrderAvailableVO commonOrderVO = new OrderAvailableVO();
            BeanUtils.copyProperties(orderPO, commonOrderVO);
            commonOrderVO.setSenderName(userRepository.selectById(orderPO.getSenderId()).getUsername());
            commonOrderVOs.add(commonOrderVO);
        }
        return commonOrderVOs;
    }

    @Override
    public OrderDetailVO getOrderDetail(Long orderId) throws JsonProcessingException {

//        // 先看看redis里面有没有
//        String redis_key = "detailOrder:" + orderId;
//        String res = (String)redisTemplate.opsForValue().get(redis_key);
//
//
//        if(res != null) {
//            return mapper.readValue(res, DetailOrderVO.class);
//        }

        OrderPO orderPO = orderRepository.selectById(orderId);
        UserPO userPO = userRepository.selectById(orderPO.getSenderId());

        // 构造这个vo
        OrderDetailVO detailOrderVO = new OrderDetailVO();

        detailOrderVO.setDescription(orderPO.getDescription());
        detailOrderVO.setWhen(orderPO.getWhen());
        detailOrderVO.setLocation(orderPO.getLocation());

        detailOrderVO.setSender(userPO.getUsername());
        detailOrderVO.setPhone(userPO.getPhone());

        detailOrderVO.setComment(orderPO.getComment());
        detailOrderVO.setPrice(orderPO.getPrice());

//        redisTemplate.opsForValue().set(redis_key,mapper.writeValueAsString(detailOrderVO));
        return detailOrderVO;
    }

    //

    @Override
    public List<OrderPublishedVO> getOrdersBySenderId(long userId) {
        List<OrderPO> orders = orderRepository.selectList(new QueryWrapper<OrderPO>().eq("sender_id", userId));
        List<OrderPublishedVO> res = new ArrayList<>();
        for (OrderPO orderPO : orders) {
            OrderPublishedVO orderVO = new OrderPublishedVO();
            BeanUtils.copyProperties(orderPO, orderVO);
            Long receiverId = orderPO.getReceiverId();
            String receiver = receiverId==null?null:userRepository.getUsernameById(receiverId);
            orderVO.setReceiverName(receiver);
            res.add(orderVO);
        }
        return res;
    }

    @Override
    public String publishOrder(long userId, OrderDTO orderDTO) {

        OrderPO orderPO = new OrderPO();

        BeanUtils.copyProperties(orderDTO, orderPO);

        orderPO.setSenderId(userId);
        orderPO.setStatus("未接单");
        orderPO.setCreateTime(new Date());

        orderRepository.insert(orderPO);
        return "success";
    }

    @Override
    public void cancelPublishOrder(long orderId) {
        orderRepository.deleteById(orderId);
    }

    @Override
    public void confirmOrder(long orderId) {
        orderRepository.confirmOrder(orderId);
    }

    //

    @Override
    public List<OrderPickedVO> getPickedOrdersByReceiverId(long userId) {
        List<OrderPO> orders = orderRepository.selectList(new QueryWrapper<OrderPO>().lambda().eq(OrderPO::getReceiverId, userId));

        List<OrderPickedVO> res = new ArrayList<>();
        for (OrderPO orderPO : orders) {
            OrderPickedVO orderVO = new OrderPickedVO();
            BeanUtils.copyProperties(orderPO,orderVO);
            String senderName = userRepository.getUsernameById(orderPO.getSenderId());
            orderVO.setSenderName(senderName);
            res.add(orderVO);
        }
        return res;
    }

    @Override
    public void pickOrder(long userId, long orderId) {
        orderRepository.pickOrder(userId, orderId);
    }

    @Override
    public void cancelPickOrder(long orderId) {
        orderRepository.cancelPickOrder(orderId);
    }

    @Override
    public void confirmOrderArrived(long orderId) {
        orderRepository.confirmOrderArrived(orderId);
    }

}
